package com.biostime.qdingding.modules.qd.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.biostime.qdingding.common.service.CrudService;
import com.biostime.qdingding.modules.qd.api.dto.MessageDto;
import com.biostime.qdingding.modules.qd.dao.MessageDao;
import com.biostime.qdingding.modules.qd.entity.Message;
import com.biostime.qdingding.modules.qd.push.PushMessage;
import com.biostime.qdingding.modules.qd.push.PushService;
import com.biostime.qdingding.modules.sys.entity.User;

/**
 * 会员Service
 * @author phil
 * @version 2015-11-06
 */
@Service
@Transactional(readOnly = true)
public class MessageService extends CrudService<MessageDao, Message> {
	
	@Autowired
	private MessageDao messageDao;
		
	/**
	 * 日志对象
	 */
	protected Logger logger = LoggerFactory.getLogger("msg");
	
	public Message send(String category, String objectId, String title, String msg, String userId, String pushMsg){
		Message message = new Message();
		
		try{
			message.setPushMsg(pushMsg);
			message.setTitle(title);
			message.setMessage(msg);
			message.setReaded(0); 
			message.setReadon(new Date()); 
			message.setCategory(category);
			
			if(!StringUtils.isEmpty(objectId))
				message.setObjectId(Integer.parseInt(objectId));
			
			message.setCreateBy(new User("0"));
			message.setUpdateBy(new User("0"));
			message.setDelFlag("0");
			message.setCreateDate(new Date());
			message.setUpdateDate(new Date());
			message.setSentBy(0);
			message.setSentTime(new Date());
			
			message.setUserId(userId);
			message.setPushMsg(pushMsg);
			
			dao.insert(message);
		}catch(Exception ex){
			ex.printStackTrace();
			logger.error(String.format("插入消息异常:[category:%s,objectId:%s,title:%s,msg:%s]", category,objectId,title,msg), ex);
		}
		
		return message;
	}
	
	public PushMessage push(Message msg){
		PushMessage pushMsg = new PushMessage();
		try{
			pushMsg.setId(msg.getId());
			pushMsg.setCategory(msg.getCategory());
			pushMsg.setTitle(msg.getTitle());
			pushMsg.setContent(msg.getPushMsg());
			pushMsg.setCleared(true);
			pushMsg.setRedirect(msg.getRedirct());
			pushMsg.setTime(msg.getCreateDate());	
			
			PushService.pushMessageToSingle(msg.getUserId(), pushMsg);	
		}catch(Exception ex){
			ex.printStackTrace();
			
			logger.error(String.format("推送消息异常:[category:%s,objectId:%s,title:%s,msg:%s]", msg.getCategory(),msg.getObjectId(),msg.getTitle(),msg.getMessage()), ex);
		}
		
		return pushMsg;
	}
	
	/**
	 * 发送并推送消息
	 */
	public void sendAndPush(String category, String objectId, String title, String msg, String userId, String pushMsg){
		Message message = send(category, objectId, title, msg, userId, pushMsg);
		push(message);
	}
	
//	 <!-- ————————————————————————————楚河汉界，谁敢动我的，打死! ————————————————————————————————————-->	
	public List<MessageDto> apiGetMessages (String studentId,String type){
		return messageDao.apiGetMessages(studentId,type);
	}
	
	
	public int apiInsertMessage(String category,String objectId,String news){
		
		Message message = new Message();
		
		//按类型填写标题和内容
		switch (category){
		case "ordered" :
			message.setTitle("预约成功");
			message.setMessage(news); 
			break;
			
		case "queuing" :
			message.setTitle("等位中");
			message.setMessage(news); 
			break;
			
		case "leaved" :
			message.setTitle("请假成功");
			message.setMessage(news); 
			break;
			
		case "leavePrivilege" :
			message.setTitle("返款特权");
			message.setMessage(news); 
			break;	
			
		case "abortPrivilege" :
			message.setTitle("返款特权");
			message.setMessage(news); 
			break;				
			
		case "cancelOrder" :
			message.setTitle("取消预约");
			message.setMessage(news); 
			break;		
			
		case "cancelOrderAfter" :
			message.setTitle("预约成功");
			message.setMessage(news); 
			break;					
			
		case "cancelQueuing" :
			message.setTitle("取消等位");
			message.setMessage(news); 
			break;	
			
		case "abort" :
			message.setTitle("预约中止");
			message.setMessage(news); 
			break;	
			
		default:
			message.setTitle("默认");
			message.setMessage("默认");
			
		}
		
		message.setReaded(0); 
		message.setReadon(new Date()); 
		message.setCategory(category);
		message.setObjectId(Integer.valueOf(objectId));
		message.setCreateBy(new User("0"));
		message.setUpdateBy(new User("0"));
		message.setDelFlag("0");
		message.setCreateDate(new Date());
		message.setUpdateDate(new Date());
		message.setSentBy(0);
		message.setSentTime(new Date()); 	
		messageDao.apiInsertMessage(message);
		
		return Integer.parseInt(message.getId());
	}
		
	public int apiUpdateState(List<String> list){
		messageDao.apiUpdateState(list);		 		
		return 0;
	}	
	
	public int apiGetMessageCount(String studentId){
		return dao.apiGetMessageCount(studentId);
	}

}
